//
//  TranManagerVC.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2020/8/4.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "TranManagerVC.h"
#import "TranManager_NavBar.h"
#import "TranManagerCell.h"
#import "HeTongManagerVC.h"
#import "DriverManagerListVC.h"
#import "CarManagerListVC.h"
#import "TiDanManagerVC.h"
#import "OperationBillManagerMainVC.h"
#import "StaffMainModel.h"
#import "TaskManagerMainVC.h"

@interface TranManagerVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) TranManager_NavBar *navBar;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray <StaffAuthModel *>*authArray;

@end

@implementation TranManagerVC

-(NSArray<StaffAuthModel *> *)authArray {
    if (!_authArray) {
        _authArray = [NSArray array];
    }
    return _authArray;
}

-(TranManager_NavBar *)navBar {
    if (!_navBar) {
        _navBar = [TranManager_NavBar new];
        _navBar.isDanRen = [User userIsDriver];
        _navBar.frame = CGRectMake(0, 0, ScreenW, self.navBar.height);
        __weak_self;
        _navBar.taskBlock = ^{
            //我的任务
            TaskManagerMainVC *vc = [TaskManagerMainVC new];
            vc.hidesBottomBarWhenPushed = YES;
            [weakself.rt_navigationController pushViewController:vc animated:YES];
        };
        _navBar.heTongBlock = ^{
            //合同管理
            if ([weakself authCan:@"11"]) {
                HeTongManagerVC *vc = [HeTongManagerVC new];
                vc.hidesBottomBarWhenPushed = YES;
                [weakself.rt_navigationController pushViewController:vc animated:YES];
            }else {
                [MBProgressHUD leShowFailedHUD:weakself.view message:@"暂无权限"];
            }
        };
        _navBar.siJiBlock = ^{
            //司机管理
            if ([weakself authCan:@"12"]) {
                DriverManagerListVC *vc = [DriverManagerListVC new];
                vc.hidesBottomBarWhenPushed = YES;
                [weakself.rt_navigationController pushViewController:vc animated:YES];
            }else {
                [MBProgressHUD leShowFailedHUD:weakself.view message:@"暂无权限"];
            }
        };
        _navBar.cheLiangBlock = ^{
            //车辆管理
            if ([weakself authCan:@"13"]) {
                CarManagerListVC *vc = [CarManagerListVC new];
                vc.hidesBottomBarWhenPushed = YES;
                [weakself.rt_navigationController pushViewController:vc animated:YES];
            }else {
                [MBProgressHUD leShowFailedHUD:weakself.view message:@"暂无权限"];
            }
        };
//        _navBar.tiDanBlock = ^{
//            if ([weakself authCan:@"6"]) {
//                TiDanManagerVC *vc = [TiDanManagerVC new];
//                vc.hidesBottomBarWhenPushed = YES;
//                [weakself.rt_navigationController pushViewController:vc animated:YES];
//            }else {
//                [MBProgressHUD leShowFailedHUD:weakself.view message:@"暂无权限"];
//            }
//        };
//        _navBar.caoZuoDanBlock = ^{
//            if ([weakself authCan:@"7"]) {
//                OperationBillManagerMainVC *vc = [OperationBillManagerMainVC new];
//                vc.hidesBottomBarWhenPushed = YES;
//                [weakself.rt_navigationController pushViewController:vc animated:YES];
//            }else {
//                [MBProgressHUD leShowFailedHUD:weakself.view message:@"暂无权限"];
//            }
//        };
    }
    return _navBar;
}

-(UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, NaviHeight + 111)];
        _bgView.backgroundColor = Color_ThemeColor;
        [_bgView setRadianLayer:DXRadianDirectionBottom Radius:20];
    }
    return _bgView;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.navBar.nim_bottom, ScreenW, ScreenH - TabbarHeight - self.navBar.nim_bottom)];
        _tableView.backgroundColor = HexColor(0xffffff);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = HexColor(0xffffff);
    [self.view addSubview:self.bgView];
    [self.view addSubview:self.navBar];
    [self.view addSubview:self.tableView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requestForAuth];
}

- (void)requestForAuth {
    [HttpRequest PostWithApi:LogisticsSupplierUserAuth params:@{} progress:^(NSProgress * _Nonnull progress) {
            
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.authArray = [StaffAuthModel mj_objectArrayWithKeyValuesArray:responseObject];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

- (BOOL)authCan:(NSString *)authId {
    __block BOOL canGo = NO;
    [self.authArray enumerateObjectsUsingBlock:^(StaffAuthModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([authId isEqualToString:obj.Id]) {
            if (obj.isauth) {
                canGo = YES;
            }
        }
    }];
    return canGo;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TranManagerCell *cell = [TranManagerCell TranManagerCellWithTableView:tableView];
    __weak_self;
    cell.feiYongChaXunBlock = ^{
        //操作单费用查询
        if ([weakself authCan:@"7"]) {
            
        }
    };
    cell.kaiPiaoChaXunBlock = ^{
        //开票查询
        if ([weakself authCan:@"9"]) {
            
        }
    };
    cell.yunFeiJieSuanBlock = ^{
        //运费结算
        if ([weakself authCan:@"10"]) {
            
        }
    };
    cell.heTongGuanLiBlock = ^{
        //合同管理
        if ([weakself authCan:@"11"]) {
            HeTongManagerVC *vc = [HeTongManagerVC new];
            vc.hidesBottomBarWhenPushed = YES;
            [weakself.rt_navigationController pushViewController:vc animated:YES];
        }else {
            [MBProgressHUD leShowFailedHUD:weakself.view message:@"暂无权限"];
        }
    };
    cell.siJiGuanLiBlock = ^{
        //司机管理
        if ([weakself authCan:@"12"]) {
            DriverManagerListVC *vc = [DriverManagerListVC new];
            vc.hidesBottomBarWhenPushed = YES;
            [weakself.rt_navigationController pushViewController:vc animated:YES];
        }else {
            [MBProgressHUD leShowFailedHUD:weakself.view message:@"暂无权限"];
        }
    };
    cell.cheLiangGuanLiBlock = ^{
        //车辆管理
        if ([weakself authCan:@"13"]) {
            CarManagerListVC *vc = [CarManagerListVC new];
            vc.hidesBottomBarWhenPushed = YES;
            [weakself.rt_navigationController pushViewController:vc animated:YES];
        }else {
            [MBProgressHUD leShowFailedHUD:weakself.view message:@"暂无权限"];
        }
    };
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    TranManagerCell *cell = [TranManagerCell TranManagerCellWithTableView:tableView];
    return cell.height;
}

@end
